/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filter;

import DatabaseAccessing.Customer;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author eisa
 */
public class SiteFilter implements Filter {


    @Override
    public void init(FilterConfig fc) throws ServletException {
      //  throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {

        HttpSession  session= ((HttpServletRequest)sr).getSession();
        Customer customers=(Customer)session.getAttribute("LOGIN_USER");
        RequestDispatcher rq=((HttpServletRequest)sr).getRequestDispatcher("Error.jsp");
        if(customers != null)
            fc.doFilter(sr, sr1);
        else
            {
              ((HttpServletRequest)sr).setAttribute("ErrorMsg","Contact Admin to acess this page");
               rq.forward(sr, sr1);
            }

        //System.out.println("Custmoer null");
        // throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void destroy() {

        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
